package com.vincent.algorithms;

import java.util.Arrays;
import java.util.Random;

/**
 * Created by yangwenchang on 2017/8/22.
 */
public class Main {


    /**
     *
     * 比较两种方式 后一种耗时要更短 差别不算太大  一般情况下可忽略  要求极致的情况下可考虑
     *
     * 100万整数比较
     * A方式耗时：22
     * B方式耗时：7
     *
     */
    public static void compare(){
        int[] arr = createRandom(1000000);
        int min = arr[0];
        int max = arr[0];
        long startTimeA = System.currentTimeMillis();
        for (int i=0;i<arr.length;i++){
            min = Math.max(min,arr[i]);
            max = Math.max(max,arr[i]);
        }
        long endTimeA = System.currentTimeMillis();
        int[] arrb = createRandom(1000000);
        int minb = arr[0];
        int maxb = arr[0];
        long startTimeB = System.currentTimeMillis();
        for (int i=0;i<arrb.length;i++){
            if(minb<arrb[i]){
                minb = arrb[i];
            }
            if(maxb<arrb[i]){
                maxb = arrb[i];
            }
        }
        long endTimeB = System.currentTimeMillis();
        System.out.println("A方式耗时："+(endTimeA-startTimeA));
        System.out.println("B方式耗时："+(endTimeB-startTimeB));
    }


    public static void main(String[] args) {


    }

    public static int[] createRandom(int size){

        long startTime = System.currentTimeMillis();
        int[] arr = new int[size];
        Random random = new Random();
        for (int i=0;i<size;i++){
            arr[i] = random.nextInt(10000);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("生成花费时间："+(endTime-startTime));
        return arr;
    }

    /**
     * 返回一个定长的随机字符串(只包含大小写字母、数字)
     *
     * @param length 随机字符串长度
     * @return 随机字符串
     */
    public static String generateString(int length) {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
//            sb.append(allChar.charAt(random.nextInt(allChar.length())));
        }
        return sb.toString();
    }
}
